package com.quasar.hpatient.view.community;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;

import com.quasar.hpatient.R;
import com.quasar.hpatient.base.BaseActivity;
import com.quasar.hpatient.choicePhoto.PhotoSelectorActivity;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.controllermodel.PictureConstant;
import com.quasar.hpatient.model.custommodel.PhotoModel;
import com.quasar.hpatient.model.event.UpdateEvent;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.onInterface.OnPostForumListener;
import com.quasar.hpatient.presenter.ForumPresenter;
import com.quasar.hpatient.view.ImageViewpageActivity_;
import com.quasar.hpatient.view.adapter.MyGrideViewAdapter;
import com.quasar.hpatient.widget.AutoHeightGridView;
import com.vise.log.ViseLog;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户发帖
 */
@OptionsMenu(R.menu.menu_submit)
@EActivity(R.layout.activity_post)
public class PostActivity extends BaseActivity implements OnPostForumListener,PictureConstant.updateUiInit {

    private ForumPresenter forumPresenter;
    MyGrideViewAdapter myGrideViewAdapter;
    public static final int PHOTO_MAX_COUNT = 100;//设置选中图片的最大数量
    private int SELECT_IMAGE_CODE = 101;
    public static ArrayList<String> photos = new ArrayList<>();
    //保存当如化验单图片
    public static ArrayList<PhotoModel> minitDatas = new ArrayList();
    PictureConstant pictureConstant;
    private final String SATTS="PostActivity";

    @ViewById(R.id.et_content)
    EditText et_content;
    @ViewById(R.id.gridView)
    AutoHeightGridView gridView;
    String cover="";

    @AfterViews
    void init(){
        initView();
        initData();
    }

    @Override
    protected void initView() {
        initToolbar();
        myGrideViewAdapter = new MyGrideViewAdapter(this, minitDatas);
        gridView.setAdapter(myGrideViewAdapter);
        pictureConstant=new PictureConstant(this);


        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == minitDatas.size()) {
                    selectPic();
                } else {
                    //跳转图片预览界面
                    //传递标识显示隐藏删除图片按钮
                    String stats = "TestRecordActivity";
                    ImageViewpageActivity_.intent(PostActivity.this).Photolist(photos).position(position).STATS(SATTS).photoModelDatas(minitDatas).start();
                }

            }
        });
    }

    //选择图片
    public void selectPic() {
        Intent intent = new Intent(this, PhotoSelectorActivity.class);
        intent.putExtra(PhotoSelectorActivity.KEY_MAX, PHOTO_MAX_COUNT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

        if (minitDatas.size() > 0) {
            intent.putExtra("datas", minitDatas);
        }
        startActivityForResult(intent, SELECT_IMAGE_CODE);

    }

    //提交
    @OptionsItem(R.id.action_submit)
    public void action_submit() {

        String count=et_content.getText().toString().trim();
        if(count==null || count.length()==0){
            count="";
        }


        if(count.length()==0 && photos.size()==0){
            msg("请填写内容");
            return;
        }

        showDialog();
        if(photos.size()!=0){
            List<File> fileList=new ArrayList<>();
            for(int i=0;i<photos.size();i++){
                fileList.add(new File(photos.get(i)));
            }
            pictureConstant.PostPicture(fileList);
        }else{
            forumPresenter.postForumData(count,cover);
        }
    }

    @Override
    protected void initData() {
        forumPresenter=new ForumPresenter(this);
    }

    @Override
    public void onPostSuccess(DataStateResult data) {
        if(data.getMsgCode().equals("0x00000000")){
            msg("发表成功");
            updateView();
        }
    }

    @Override
    public void onPostError(String msg) {

        if(msg!=null){
            msg(msg);
        }
        dessDialog();
    }

    @Override
    public void onPostRefreshUI() {
        dessDialog();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != Activity.RESULT_OK)
            return;
        if (requestCode == SELECT_IMAGE_CODE) {// selected image
            if (data != null && data.getExtras() != null) {
                List<PhotoModel> photosModel = (List<PhotoModel>) data.getExtras()
                        .getSerializable("photos");
                if (photosModel == null || photosModel.isEmpty()) {
                } else {
                    photos.clear();
                    minitDatas.clear();
                    ViseLog.d("图片的数据长度" + photosModel.size());
                    minitDatas.addAll(photosModel);
//                    myGrideViewAdapter.notifyDataSetChanged();

                    for (int i = 0; i < photosModel.size(); i++) {
                        photos.add(photosModel.get(i).getOriginalPath());
                    }
                }
                myGrideViewAdapter.notifyDataSetChanged();
            }

        }

    }

    @Override
    public void updateInit(String path) {

        String count=et_content.getText().toString().trim();
        if(count==null || count.length()==0){
            count="";
        }
        if(path.length()>0){
            forumPresenter.postForumData(count,path);
        }
    }

    @Override
    public void onErrPhoto(String errVal) {
        if(errVal!=null){
            msg("图片提交失败");
        }
        dessDialog();
    }

    public void updateView(){
        UpdateEvent updateEvent = new UpdateEvent();
        updateEvent.type = ConstantManger.UPDATEPOST;
        EventBus.getDefault().post(updateEvent);
        finish();
    }
}
